package controllers

import dao._
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, ControllerComponents}
import play.api.routing.JavaScriptReverseRouter
import tool.{FormTool, Tool}

import javax.inject.Inject
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

/**
 * Created by Administrator on 2019/8/7
 */
class AppController @Inject()(cc: ControllerComponents)(
  implicit val accountDao: AccountDao,
  implicit val userDao: UserDao,
  implicit val memberDao: MemberDao,
  implicit val companyMemberDao: CompanyMemberDao,
  implicit val specialMemberDao: SpecialMemberDao,
) extends
  AbstractController(cc) {

  def loginApi = Action.async { implicit request =>
    val data = FormTool.memberForm.bindFromRequest().get
    val rolesF = Future.sequence(
      List(
        memberDao.selectGetRole(data.account, data.password),
        specialMemberDao.selectGetRole(data.account, data.password),
        companyMemberDao.selectGetRole(data.account, data.password),
      )
    )
    accountDao.selectById1.zip(rolesF).map { case (account, roleOps) =>
      val json = if (data.account == account.account && data.password == account.password) {
        Json.obj("valid" -> true, "role" -> "admin")
      } else {
        val roleOpOp = roleOps.find(_.isDefined)
        roleOpOp match {
          case Some(roleOp) =>
            Json.obj("valid" -> true, "role" -> roleOp.get)
          case None =>
            Json.obj("valid" -> false)
        }
      }

      Ok(json)
    }
  }

  def changePassword = Action.async { implicit request =>
    val data = FormTool.changePasswordForm.bindFromRequest().get
    val account = FormTool.accountForm.bindFromRequest().get.account
    Tool.updatePassword(account, data).map { b =>
      if (b) {
        Ok(Json.obj("valid" -> true))
      } else {
        Ok(Json.obj("valid" -> false))
      }
    }
  }

  def javascriptRoutes = Action { implicit request =>
    Ok(
      JavaScriptReverseRouter("jsRoutes")(

        controllers.routes.javascript.MemberController.accountCheck,
        controllers.routes.javascript.MemberController.add,
        controllers.routes.javascript.MemberController.all,
        controllers.routes.javascript.MemberController.delete,
        controllers.routes.javascript.MemberController.getById,
        controllers.routes.javascript.MemberController.update,

        controllers.routes.javascript.CompanyMemberController.add,
        controllers.routes.javascript.CompanyMemberController.all,
        controllers.routes.javascript.CompanyMemberController.delete,
        controllers.routes.javascript.CompanyMemberController.getById,
        controllers.routes.javascript.CompanyMemberController.update,

        controllers.routes.javascript.SpecialMemberController.all,
        controllers.routes.javascript.SpecialMemberController.add,
        controllers.routes.javascript.SpecialMemberController.delete,
        controllers.routes.javascript.SpecialMemberController.getById,
        controllers.routes.javascript.SpecialMemberController.update,

        controllers.routes.javascript.WechatMemberController.all,
        controllers.routes.javascript.WechatMemberController.phoneCheck,
        controllers.routes.javascript.WechatMemberController.add,
        controllers.routes.javascript.WechatMemberController.delete,

        controllers.routes.javascript.AppController.changePassword,

        controllers.routes.javascript.SampleIdController.getNewIds,
        controllers.routes.javascript.SampleIdController.add2Db,

        controllers.routes.javascript.SampleController.add,
        controllers.routes.javascript.SampleController.all,
        controllers.routes.javascript.SampleController.delete,
        controllers.routes.javascript.SampleController.getById,
        controllers.routes.javascript.SampleController.update,
        controllers.routes.javascript.SampleController.idCheck,
        controllers.routes.javascript.SampleController.updateWorkflow,
        controllers.routes.javascript.SampleController.getByIdNoImage,

        controllers.routes.javascript.ToolController.viewFile,
        controllers.routes.javascript.ToolController.uploadFile,

      )
    ).as("text/javascript")

  }


}
